package self.hoy.demo.grainmall.product.app;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import self.hoy.demo.common.valid.Add;
import self.hoy.demo.common.valid.Switch;
import self.hoy.demo.common.valid.Update;
import self.hoy.demo.grainmall.product.entity.Brand;
import self.hoy.demo.grainmall.product.service.BrandService;
import self.hoy.demo.common.utils.PageUtils;
import self.hoy.demo.common.utils.R;


/**
 * 品牌
 *
 * @author Hoy
 * @email xxx@gmail.com
 * @date 2020-07-04 19:07:56
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		Brand brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(/*@Valid*/@Validated(Add.class) @RequestBody Brand brand/*, BindingResult result*/){
//        if (result.hasErrors() ){
//            Map<String, String> map = new HashMap<>();
//            result.getFieldErrors().forEach((item)->{
//                String fieldName = item.getField();
//                String message = item.getDefaultMessage();
//                map.put(fieldName, message);
//            });
//            return R.error(400, "").put("data", map);
//        }
		brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(/*@Valid*/@Validated(Update.class) @RequestBody Brand brand/*, BindingResult result*/){
//        if (result.hasErrors()) {
//            Map<String, String> map = new HashMap<>();
//            result.getFieldErrors().forEach((item)->{
//                String fieldName = item.getField();
//                String message = item.getDefaultMessage();
//                map.put(fieldName, message);
//            });
//            return R.error(400, "").put("data", map);
//        }
		brandService.updateDetails(brand);
        return R.ok();
    }

    @PostMapping("/switch")
    public R switchStatus(@Validated(Switch.class) @RequestBody Brand brand) {
        brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
